/**
 * jims
 */
package com.jims.master.modules.csm.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.ChineseCharToEnUtils;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.csm.dao.ExpStorageDeptDao;
import com.jims.master.modules.csm.entity.ExpStorageDept;
import com.jims.master.modules.sys.dao.OrgDeptDao;
import com.jims.master.modules.sys.entity.OrgDept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 消耗品库存单位字典表Service
 *
 * @author DT
 * @version 2017-03-09
 */
@Service
@Transactional(readOnly = true)
public class ExpStorageDeptService extends CrudService<ExpStorageDeptDao, ExpStorageDept> {
    @Autowired
    private OrgDeptDao orgDeptDao;

    public ExpStorageDept get(String id) {
        return super.get(id);
    }

    public List<ExpStorageDept> findList(ExpStorageDept expStorageDept) {
        return super.findList(expStorageDept);
    }

    public Page<ExpStorageDept> findPage(Page<ExpStorageDept> page, ExpStorageDept expStorageDept) {
        expStorageDept.updateOrgId();
        return super.findPage(page, expStorageDept);
    }

    @Transactional(readOnly = false)
    public int delete(ExpStorageDept expStorageDept) {
        return super.delete(expStorageDept);
    }

    /**
     * 消耗品库存单位自动补全
     *
     * @param expStorageDept
     * @param q
     * @param limit
     * @return
     * @author ZHUQ
     * @date 2017-03-12 15:08:59
     */
    public List<ExpStorageDept> findAutoCompleteList(ExpStorageDept expStorageDept, String q, String limit) {
        expStorageDept.updateOrgId();
        return dao.findAutoCompleteList(expStorageDept, q, limit);
    }

    /**
     * 消耗品库存单位自动补全、根据入库方式自动选择是库房还是厂商
     *
     * @param expStorageDept
     * @param q
     * @param limit
     * @return
     * @author ZHUQ
     * @date 2017-03-12 15:08:59
     */
    public List<ExpStorageDept> findByMasterClass(ExpStorageDept expStorageDept, String q, String limit, String
            masterClass) {
        expStorageDept.updateOrgId();
        List<ExpStorageDept> expStorageDeptList = new ArrayList<ExpStorageDept>();
        if ("采购".equals(masterClass)) {
            expStorageDeptList = dao.findAutoCompleteBySupplier(expStorageDept, q, limit);
        } else {
            expStorageDeptList = dao.findAutoCompleteList(expStorageDept, q, limit);
        }
        return expStorageDeptList;
    }

    /**
     * 根据科室ID查询库房信息
     *
     * @param storageCode 库房表exp_storage_dept的storageCode,是科室表org_dept的ID
     * @return
     * @author fengyg
     */
    public ExpStorageDept getByStorageCode(String storageCode) {
        return dao.getByStorageCode(storageCode);
    }

    /**
     * 耗材库存单位保存
     * @param expStorageDept
     * @return
     */
    @Transactional(readOnly = false)
    public int saveStorage(ExpStorageDept expStorageDept) {
        int i = 0;
        ExpStorageDept storageList = new ExpStorageDept();
        storageList.updateOrgId();
        List<ExpStorageDept> list = dao.findList(storageList);
        if(null != list && list.size() > 0){
            for (ExpStorageDept storageDept : list) {
                if(expStorageDept.getDisburseNoPrefix().equals(storageDept.getDisburseNoPrefix())){ //付款单前缀已经存在
                    return -1;
                }
                if(expStorageDept.getExportNoPrefix().equals(storageDept.getExportNoPrefix())){  //出库单号前缀已经存在
                    return -3;
                }
                if(expStorageDept.getImportNoPrefix().equals(storageDept.getImportNoPrefix())){  //入库单号前缀已经存在
                    return -2;
                }
            }
        }
        OrgDept orgDept = new OrgDept();
        if (StringUtils.isBlank(expStorageDept.getStorageName())) {
            if (StringUtils.isNotBlank(expStorageDept.getStorageCode())) {//根据字节码判断是否做删除还是添加
                orgDept.setId(expStorageDept.getStorageCode());
                orgDept.updateOrgId();
                orgDept = orgDeptDao.findId(orgDept);
                expStorageDept.setStorageName(orgDept.getDeptName());
                expStorageDept.setInputCode(ChineseCharToEnUtils.getAllFirstLetter(expStorageDept.getStorageName()));
                expStorageDept.setStorageCode(orgDept.getId());
            }
            expStorageDept.preInsert();
            expStorageDept.updateOrgId();
            expStorageDept.setId(expStorageDept.getStorageCode());
            i = dao.insert(expStorageDept);
        } else {
            expStorageDept.preUpdate();
            expStorageDept.setInputCode(ChineseCharToEnUtils.getAllFirstLetter(expStorageDept.getStorageName()));
            i = dao.update(expStorageDept);
        }
        return i;
    }

}